var request = require('../../utils/request.js')
var util = require('../../utils/util.js')
Page({
    data: {
        noticeShow: true,
        notice: '',
        nowStart: '',
        nowEnd: '',
        wantNotice: '',
        today: util.formatDate(new Date()),
        sdate: util.formatDate(new Date()),
        stime: '00:00',
        edate: util.formatDate(new Date()),
        etime: '23:59'
    },
    onLoad(){
        this.getNotice()
    },
    // 获取通知
    getNotice(){
        let that = this
        request.GET('/api/broadcastlatest/').then(res=>{
            console.log(res)
            if(!res.data.value){
                this.setData({
                    noticeShow: false
                })
                qq.showToast({
                    title: '当前无通知',
                    icon: 'none'
                })
            } else {
                that.setData({
                    noticeShow: true,
                    notice: res.data.value,
                    nowStart: res.data.starttime,
                    nowEnd: res.data.endtime,
                })
            }
        })
    },
    // 关闭通知
    noticeClose(){
        this.setData({
            noticeShow: false
        })
    },
    // 通知
    noticeInput(e) {
        this.setData({
            wantNotice: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    noticeSubmit() {
        if(this.data.wantNotice==''){
            qq.showToast({
                title: '请先输入内容',
                icon: 'none'
            })
            return
        }
        let that = this
        let req = {
            "starttime": this.data.sdate+'T'+this.data.stime+':00',
            "endtime": this.data.edate+'T'+this.data.etime+':59',
            "value": this.data.wantNotice
        }
        request.POST('/api/broadcast/', req).then(res => {
            that.getNotice()
            qq.showToast({
                title: '发布成功',
                icon: 'none'
            })
        })
    },
    //关闭所有通知 当前接口发布过期通知即可关闭通知
    closeNotice(){
        let that = this
        let req = {
            "starttime": '2021-08-18T00:00:00',
            "endtime": '2021-08-18T23:59:59',
            "value": "关闭当前所有通知"
        }
        request.POST('/api/broadcast/', req).then(res => {
            that.getNotice()
            qq.showToast({
                title: '发布成功',
                icon: 'none'
            })
        })
    },
    // 时间选择
    bindSDateChange(e){
        this.setData({
            sdate: e.detail.value
        })
    },
    bindSTimeChange(e){
        this.setData({
            stime: e.detail.value
        })
    },
    bindEDateChange(e){
        this.setData({
            edate: e.detail.value
        })
    },
    bindETimeChange(e){
        this.setData({
            etime: e.detail.value
        })
    },
})